#include "modulefileconfig.h"

#include <QDebug>

#include <QDir>
#include <QtXml>
#include <QMessageBox>
#include <QDateTime>
#include <QRandomGenerator>
#include <QFile>

moduleFileConfig::moduleFileConfig(QWidget *parent) : QWidget(parent)
{
}

void moduleFileConfig::init_Module_Data(QString path)
{
    qDebug() << "moduleFileConfig init module data" << path;

    QFile fp(path);
    if (!fp.open(QFile::ReadOnly))
    {
        qDebug() << fp.error() << fp.errorString();
        QMessageBox::information(this, "Error", "moduleFileConfig Can't Read Project File", QMessageBox::Ok);
        return;
    }

    QDomDocument doc;
    if (! doc.setContent(&fp))
    {
        QMessageBox::information(this, "Error", "moduleFileConfig Can't Read Project File", QMessageBox::Ok);
        return;
    }

    fp.close();

    QDomElement root = doc.documentElement();
    this->Data->set_module_name(root.attribute("name"));

    QDomElement port = doc.elementsByTagName("port").at(0).toElement();

    QDomNodeList port_list = port.childNodes();

    // 读取port属性

    for (int n(0); n < port_list.length(); n++) {
        QDomElement p = port_list.at(n).toElement();
        QString type = p.attribute("type");

        if (type == "Input")
        {
            PortAttribute pa;
            pa.name = p.attribute("name");
            pa.id = p.attribute("id");
            pa.type = p.attribute("type");

            qDebug() << "make" << pa.name << pa.type;

            this->Data->set_input(pa);
        }
        else if (type == "Output")
        {
            PortAttribute pa;
            pa.name = p.attribute("name");
            pa.id = p.attribute("id");
            pa.type = p.attribute("type");

            qDebug() << "make" << pa.name << pa.type;

            this->Data->set_output(pa);
        }
        else if (type == "InOut")
        {
            PortAttribute pa;
            pa.name = p.attribute("name");
            pa.id = p.attribute("id");
            pa.type = p.attribute("type");

            qDebug() << "make" << pa.name << pa.type;

            this->Data->set_inout(pa);
        }
    }
}

void moduleFileConfig::recv_newModule(QString path, Module_Data *data)
{
    this->create_moduleFile(path, data);
}

void moduleFileConfig::create_moduleFile(QString path, Module_Data *data)
{
    QString module_dirPath = path + MODULE_DIRECTORY_NAME;
    QString fPath = module_dirPath + data->get_module_name() + MODULE_END_FORMAT;

    // 检测module目录是否存在，不存在则创建目录
    this->check_moduleDictory(module_dirPath);

    QFile fp(fPath);

    if (! fp.open(QFile::ReadWrite))
    {
        QMessageBox::information(this, "Error", "Can't Create Module File", QMessageBox::Ok);
        return;
    }

    this->init_moduleFile(&fp, data);

    fp.close();
}

void moduleFileConfig::init_moduleFile(QFile *fp, Module_Data *data)
{
    QDomDocument doc;
    QDomProcessingInstruction instruction;  // 命令处理变量

    instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instruction);

    QDomElement root = doc.createElement("module");
    root.setAttribute("name", data->get_module_name());
    this->Data->set_module_name(data->get_module_name());

    QDomElement new_ports = doc.createElement("port"); // Port节点

    ///---------------------------------------------------------------
    /// 创建Input Port节点

    auto ports = data->ports.Input_List;

    for (int n(0); n < data->get_input(); n++) {
        QDomElement port = doc.createElement("input");

        PortAttribute pa;
        pa.name = ports.at(n).name;
        pa.id = ports.at(n).id;
        pa.type = ports.at(n).type;

        port.setAttribute("name", pa.name);
        port.setAttribute("id", pa.id);
        port.setAttribute("type", pa.type);

        new_ports.appendChild(port);

        new_ports.appendChild(port);
    }

    ///---------------------------------------------------------------
    /// 创建Output Port节点

    ports = data->ports.Output_List;

    for (int n(0); n < data->get_output(); n++) {
        QDomElement port = doc.createElement("output");

        PortAttribute pa;
        pa.name = ports.at(n).name;
        pa.id = ports.at(n).id;
        pa.type = ports.at(n).type;

        port.setAttribute("name", ports.at(n).name);
        port.setAttribute("id", ports.at(n).id);
        port.setAttribute("type", ports.at(n).type);

        new_ports.appendChild(port);

        new_ports.appendChild(port);
    }

    ///---------------------------------------------------------------
    /// 创建InOut Port节点

    ports = data->ports.InOut_List;

    for (int n(0); n < data->get_inout(); n++) {
        QDomElement port = doc.createElement("inout");

        PortAttribute pa;
        pa.name = ports.at(n).name;
        pa.id = ports.at(n).id;
        pa.type = ports.at(n).type;

        port.setAttribute("name", ports.at(n).name);
        port.setAttribute("id", ports.at(n).id);
        port.setAttribute("type", ports.at(n).type);

        new_ports.appendChild(port);

        new_ports.appendChild(port);
    }

    root.appendChild(new_ports);
    doc.appendChild(root);

    QTextStream out(fp);
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
    out.setCodec("utf-8");
#endif
    doc.save(out, 4);   // 缩减4格并保存
}

void moduleFileConfig::check_moduleDictory(QString path)
{
    QDir dir;

    if (! dir.exists(path))
    { dir.mkpath(path); }
}


Module_Data *moduleFileConfig::get_module_data()
{
    return this->Data;
}

QString moduleFileConfig::get_module_name()
{
    return this->Data->get_module_name();
}
